package cloudspace.controlpanel.filemanager;

import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.TreeitemRenderer;
import org.zkoss.zul.Treerow;

import cloudspace.vm.filesystem.CSPath;


// ----------------------------------------------------------
public class CSPathTreeitemRenderer implements TreeitemRenderer
{
    // ----------------------------------------------------------
    public void render( Treeitem item, Object data )
    {
        CSPath file = (CSPath)data;

        Treerow tr;
        if ( item.getTreerow() == null )
        {
            tr = new Treerow();
            tr.setParent( item );
        }
        else
        {
            tr = item.getTreerow();
            tr.getChildren().clear();
        }

        Treecell cell = new Treecell();
        cell.setParent( tr );

        item.setValue( file );
        item.setOpen( false );
        item.setImage( FileTypes.getFileIcon( file ) );

        if ( file.isRoot() )
        {
            cell.setStyle( "font-weight: bold" );

            if ( file.getZone().equals( "work" ) )
            {
                cell.setLabel( file.getPath() );
            }
            else
            {
                cell.setLabel( file.getZone() + "/" + file.getPath() );
            }
        }
        else
        {
            if ( file.getParent().isRoot() )
            {
                cell.setLabel( file.getQualifiedPath() );
            }
            else
            {
                cell.setLabel( file.getName() );
            }
        }

        if ( file.isHidden() )
        {
            cell.setStyle( HIDDEN_FILE_STYLE );
        }
    }

    private static final String HIDDEN_FILE_STYLE = "opacity: 0.5";
}
